/*----------------------------------------------------------------------------
 *      RL-ARM - RTC
 *----------------------------------------------------------------------------
 *      Name:    RTX_RTC.c
 *      Purpose: RTC Generic Layer Driver
 *      Rev.:    V4.70
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *---------------------------------------------------------------------------*/
#include <time.h>
#include "RTL.h"     /* RTX kernel functions & defines      */
#include "RTC\RTX_RTC.h" /* RTC Generic functions & defines     */

#pragma diag_suppress 550

RTC_ERROR RTC_init()
{
#ifdef BSP_USE_STM32RTC
    return RTC_hw_init();
#endif
}

time_t time(time_t* t)
{
    time_t time_now = 0;
#ifdef BSP_USE_STM32RTC

    /* read timestamp from RTC device. */
    if (RTC_hw_GetCounter(&time_now) == RTC_OK)
    {
        if (t != NULL)
            *t = time_now;
    }

#endif
    return time_now;
}

/** \brief set system date(time not modify).
 *
 * \param U32 year  e.g: 2012.
 * \param U32 month e.g: 12 (1~12).
 * \param U32 day   e.g: e.g: 31.
 * \return rt_err_t if set success, return RT_EOK.
 *
 */
RTC_ERROR set_date(U32 year, U32 month, U32 day)
{
    RTC_ERROR rtc_error = RTC_OK;
#ifdef BSP_USE_STM32RTC
    time_t now;
    struct tm* p_tm;
    struct tm tm_new;

    /* get current time */
    now = time(NULL);

    /* converts calendar time time into local time. */
    p_tm = localtime(&now);
    /* copy the statically located variable */
    memcpy(&tm_new, p_tm, sizeof(struct tm));

    /* update date. */
    tm_new.tm_year = year - 1900;
    tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
    tm_new.tm_mday = day;

    /* converts the local time in time to calendar time. */
    now = mktime(&tm_new);

    rtc_error = RTC_hw_SetCounter(now);
#endif
    return rtc_error;
}

/** \brief set system time(date not modify).
 *
 * \param U32 hour   e.g: 0~23.
 * \param U32 minute e.g: 0~59.
 * \param U32 second e.g: 0~59.
 * \return rt_err_t if set success, return RT_EOK.
 *
 */
RTC_ERROR set_time(U32 hour, U32 minute, U32 second)
{
    RTC_ERROR rtc_error = RTC_OK;
#ifdef BSP_USE_STM32RTC
    time_t now;
    struct tm* p_tm;
    struct tm tm_new;

    /* get current time */
    now = time(NULL);

    /* lock scheduler. */

    /* converts calendar time time into local time. */
    p_tm = localtime(&now);
    /* copy the statically located variable */
    memcpy(&tm_new, p_tm, sizeof(struct tm));
    /* unlock scheduler. */

    /* update time. */
    tm_new.tm_hour = hour;
    tm_new.tm_min = minute;
    tm_new.tm_sec = second;

    /* converts the local time in time to calendar time. */
    now = mktime(&tm_new);

    /* update to RTC device. */
    rtc_error = RTC_hw_SetCounter(now);
#endif
    return rtc_error;
}

/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
